﻿using CMS.Tool.WebApi.Models;
using CMS.Tool.WebApi.Models.Base;
using CodeTool.WebApi.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http;

namespace CMS.Tool.WebApi.Controllers
{
    /// <summary>
    /// 代码生成器
    /// </summary>
    [RoutePrefix("api/Coder")]
    public class CoderController : ApiController
    {
        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpGet, Route("List")]
        public PostResultList<Tables> GetTables([FromUri]GetTablePage info)
        {
            return new GetData().GetTable(info);
        }

        /// <summary>
        /// 生成代码
        /// </summary>
        /// <param name="tablename"></param>
        /// <returns></returns>
        [Route("Create")]
        [HttpGet]
        public HttpResponseMessage GenerateCode(string tablename)
        {
            if (string.IsNullOrEmpty(tablename))
            {
                return new HttpResponseMessage(HttpStatusCode.NoContent);
            }

            try
            {
                Coder d = new Coder();
                var FilePath = string.Empty;
                var FileName = string.Empty;
                var res = d.Builder(tablename, out FilePath, out FileName);
                // var FilePath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/download/EditPlus64_xp85.com.zip");
                var stream = new FileStream(FilePath, FileMode.Open);
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
                response.Content = new StreamContent(stream);
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = FileName
                };
                return response;
            }
            catch (Exception e)
            {
                return new HttpResponseMessage(HttpStatusCode.InternalServerError);
            }
        }

    }
}
